<?php
namespace Common\Model;
use Think\Model;

/**
 * Class SearchModel
 * @package Home\Model
 */
class SearchModel extends Model {

    protected $trueTableName = "info_content";

    public function search($qword){
        if(!empty($qword)){
            $res = $this->sphinx($qword);
        }else{
            $res = array();
        }
        return $res;
    }


    public function sphinx($qword, $indexname="index_info", $number=50)
    {

        import("Org.Sphinxapi");
        $cl = new \SphinxClient();
        $q = $qword;
        $sql = "";
        $mode = SPH_MATCH_ANY;
        $host = "localhost";
        $port = 9312;
        $index = $indexname;//"*";
        $groupby = "";
        $groupsort = "@group desc";
        $filter = "group_id";
        $filtervals = array();
        $distinct = "";
        $sortby = "";
        $sortexpr = "";
        $limit = $number;
        $ranker = SPH_RANK_PROXIMITY_BM25;
        $select = "";
        for ( $i=0; $i<count($args); $i++ )
        {
            $arg = $args[$i];

            if ( $arg=="-h" || $arg=="--host" )             $host = $args[++$i];
            else if ( $arg=="-p" || $arg=="--port" )        $port = (int)$args[++$i];
            else if ( $arg=="-i" || $arg=="--index" )       $index = $args[++$i];
            else if ( $arg=="-s" || $arg=="--sortby" )      { $sortby = $args[++$i]; $sortexpr = ""; }
            else if ( $arg=="-S" || $arg=="--sortexpr" )    { $sortexpr = $args[++$i]; $sortby = ""; }
            else if ( $arg=="-a" || $arg=="--any" )         $mode = SPH_MATCH_ANY;
            else if ( $arg=="-b" || $arg=="--boolean" )     $mode = SPH_MATCH_BOOLEAN;
            else if ( $arg=="-e" || $arg=="--extended" )    $mode = SPH_MATCH_EXTENDED;
            else if ( $arg=="-e2" )                         $mode = SPH_MATCH_EXTENDED2;
            else if ( $arg=="-ph"|| $arg=="--phrase" )      $mode = SPH_MATCH_PHRASE;
            else if ( $arg=="-f" || $arg=="--filter" )      $filter = $args[++$i];
            else if ( $arg=="-v" || $arg=="--value" )       $filtervals[] = $args[++$i];
            else if ( $arg=="-g" || $arg=="--groupby" )     $groupby = $args[++$i];
            else if ( $arg=="-gs"|| $arg=="--groupsort" )   $groupsort = $args[++$i];
            else if ( $arg=="-d" || $arg=="--distinct" )    $distinct = $args[++$i];
            else if ( $arg=="-l" || $arg=="--limit" )       $limit = (int)$args[++$i];
            else if ( $arg=="--select" )                    $select = $args[++$i];
            else if ( $arg=="-fr"|| $arg=="--filterrange" ) $cl->SetFilterRange ( $args[++$i], $args[++$i], $args[++$i] );
            else if ( $arg=="-r" )
            {
                $arg = strtolower($args[++$i]);
                if ( $arg=="bm25" )     $ranker = SPH_RANK_BM25;
                if ( $arg=="none" )     $ranker = SPH_RANK_NONE;
                if ( $arg=="wordcount" )$ranker = SPH_RANK_WORDCOUNT;
                if ( $arg=="fieldmask" )$ranker = SPH_RANK_FIELDMASK;
                if ( $arg=="sph04" )    $ranker = SPH_RANK_SPH04;
            }
            else
                $q .= $args[$i] . " ";
        }
        // do query
        $cl->SetServer ( $host, $port );
        $cl->SetConnectTimeout ( 1 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( $mode );
        if ( count($filtervals) )   $cl->SetFilter ( $filter, $filtervals );
        if ( $groupby )             $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
        $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'create_time');
        //$cl->SetSortMode(SPH_SORT_RELEVANCE, 'app_name');
        if ( $sortby )              $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
        if ( $sortexpr )            $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
        if ( $distinct )            $cl->SetGroupDistinct ( $distinct );
        if ( $select )              $cl->SetSelect ( $select );
        if ( $limit )               $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
        $cl->SetRankingMode ( $ranker );
        $res = $cl->Query( $q );

        if(isset($res['matches']) && $res['matches']){
            $ids = array_column($res['matches'],'id');
            $word = array_keys($res['words']);
            $total_found = $res['total_found'];
        }else{
            $total_found = 0;
            $ids = array();
            $word = array();
        }
        $data = array(
            'count' => $total_found,
            'idArr' => $ids,
            'word'  => $word
        );
        return $data;
    }



}